module lib.node_modules;

import std.path : buildPath;
import std.file : exists;

/**
  Find the binary under a node_modules folder

  Params:
      nodeModulesDir = The path to the node_modules directory
      packageName = The package to find the binary for
      binaryName = The name of the binary
*/
auto findNodeModulesBin(string nodeModulesDir, string packageName, string binaryName)
{
  const hoistedPath = buildPath(nodeModulesDir, packageName, binaryName);
  auto extension = "";
  version (Windows)
  {
    extension = ".exe";
  }
  const nativeBin = hoistedPath ~ extension;
  if (nativeBin.exists)
  {
    return nativeBin;
  }
  else
  {
    return buildPath(nodeModulesDir, ".bin", binaryName);
  }
}